package main;

//Nikolas Wolfe
//2/9/2009
//CIS 4930
//Towers of Hanoi

/*
 * Dumbest possible AI
 */

public class ConcreteStupidExecution implements ExecutionStrategy
{
	private TowerWatcher c;
	
	public ConcreteStupidExecution(TowerWatcher c)
	{
		this.c = c;
	}
	
	public void run()
	{
		while( !Thread.interrupted() )
		{
			try
			{
				if( !c.getToh().checkIfSolved() ) // this will interrupt the thread if
										   				// the puzzle has been solved
				{
					// supply output methods with name
					if( c.getToh().hasLegalMove(c.getMyTower(), c.toString()) )
					{
						if(c.getToh().moveDiskRight(c.getMyTower(), c.toString())) 
						{ 
							c.setActionCount(c.getActionCount()+1);
						}
						if(c.getToh().moveDiskLeft(c.getMyTower(), c.toString() )) 
						{
							c.setActionCount(c.getActionCount()+1); 
						}
					}
					// yield if done
					c.checkToYieldAction();
					c.getToh().checkReadyToYield( c.getMyTower() );
				}
			} catch( InterruptedException e )
			{
				// set the flag to exit the while loop
				// at the start of the next iteration
				Thread.currentThread().interrupt();
			}
		}
	}
}
